package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.entity.*;
import com.zzyl.exception.BusinessException;
import com.zzyl.mapper.*;
import com.zzyl.service.RoomService;
import com.zzyl.vo.BedVo;
import com.zzyl.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    //根据type name去RoomTypeMapper中查找对应id
    @Autowired
    private RoomTypeMapper roomTypeMapper;

    //查询房间信息
    @Autowired
    private BedMapper bedMapper;

    //查看入住配置表
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;

    @Autowired
    private ElderMapper elderMapper;

    //新增或者修改房间
    @Override
    public void saveOrUpdateRoom(Room room) {
//        //缺少roomtypeid
//        LambdaQueryWrapper<RoomType> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(RoomType::getName,room.getTypeName());
//        RoomType roomType=roomTypeMapper.selectOne(wrapper);
//        //判断是否查询到roomtype
//        if (roomType==null){
//            //未查到 抛异常
//            throw new BusinessException("房间类型不存在");
//        }
//        //给room中的typeid赋值
//        room.setTypeId(roomType.getId());
//        //判断是新增还是修改  修改不传id
//        if (room.getId()==null){
//            getBaseMapper().insert(room);
//        }else {
//            getBaseMapper().updateById(room);
//        }


        //构建条件查询器
        LambdaQueryWrapper<RoomType> queryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件 room中的typename=roomtype中的name
        queryWrapper.eq(RoomType::getName, room.getTypeName());
        //根据条件查询符合要求的roomtype
        RoomType roomType = roomTypeMapper.selectOne(queryWrapper);
        //判断 如果查询到的roomtype为空 说明房型不存在 输入房型有误 抛异常
        if (roomType == null) {
            throw new BusinessException("房型有误,请重新输入");
        }
        //roomtype不为空 获取roomtype的id赋值给room的typeid
        room.setTypeId(roomType.getId());
        //调用roommapper新增房间
        if (room.getId()==null){
            getBaseMapper().insert(room);
        }else {
            getBaseMapper().updateById(room);
        }
    }

    //根据楼层查询房间
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return getBaseMapper().getRoomsByFloorId(floorId);
    }

    @Override
    public RoomVo getRoom(Long id) {
//        //1.直接查询获取房间id code sort
//        Room room=getBaseMapper().selectById(id);
//        RoomVo roomVo = BeanUtil.copyProperties(room, RoomVo.class);
//
//        //2.获取总床位数和入住床位数 计算出入住率
//        //2.1根据roomid获取当前room内床位信息
//        LambdaQueryWrapper<Bed> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Bed::getRoomId,id);
//        List<Bed> beds = bedMapper.selectList(wrapper);
//        //2.2获取总床位数(beds集合长度  已入住床位(stream流过滤获取status为1的元素
//        int totalBeds=beds.size();
//        int occupiedBeds= (int) beds.stream().filter(bed -> bed.getBedStatus()==1).count();
//        //2.3计算出入住率(double类型
//        if (totalBeds!=0&&occupiedBeds!=0){
//            roomVo.setOccupancyRate(totalBeds*1.0/occupiedBeds);
//        }else {
//            roomVo.setOccupancyRate(0.0);
//        }
//        //3.创建床位集合
//        List<BedVo>bedVoList=beds.stream().map(bed -> {
//            //创建bedvo对象
//            BedVo bedVo=new BedVo();
//            bedVo.setBedNumber(bed.getBedNumber());
//            //服务等级从check in config表中获取
//            LambdaQueryWrapper<CheckInConfig> wrapper1 = new LambdaQueryWrapper<>();
//            wrapper1.eq(CheckInConfig::getBedNumber,bed.getBedNumber());
//            //获取入住配置对象
//            CheckInConfig checkInConfig = checkInConfigMapper.selectOne(wrapper1);
//            //如果查询到的对象不为空
//            if(checkInConfig!=null){
//                bedVo.setBedNumber(checkInConfig.getBedNumber());
//                //给bedvo中的checekInConfigVo赋值 所以先get获取cifvo对象
//                bedVo.getCheckInConfigVo().setCheckInStartTime(checkInConfig.getCheckInStartTime());
//                bedVo.getCheckInConfigVo().setCheckInEndTime(checkInConfig.getCheckInEndTime());
//            }
//            //获取老人信息
//            LambdaQueryWrapper<Elder> queryWrapper2 = new LambdaQueryWrapper<>();
//            queryWrapper2.eq(Elder::getBedId,bed.getId());
//            Elder elder=elderMapper.selectOne(queryWrapper2);
//
//            if (elder!=null){
//                bedVo.setName(elder.getName());
//            }
//
//            //返回vo
//            return bedVo;
//    }).collect(Collectors.toList());
//        roomVo.setBedVoList(bedVoList);
//        return roomVo;
//
        //1.房间id 房间编号 排序 房间类型id 房间类型名称  直接查询可得
        Room room = getBaseMapper().selectById(id);
        RoomVo roomVo = BeanUtil.copyProperties(room, RoomVo.class);

        //2.根据房间id查询当前房间的床位信息(相当于join bed on roomid=id
        LambdaQueryWrapper<Bed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bed::getRoomId, id);
        //获得当前房间的床的集合
        List<Bed> beds = bedMapper.selectList(queryWrapper);

        //3.查询总床位数,入住床位数,入住率
        int totalBeds = beds.size();
        int occupiedBeds = (int) (beds.stream().filter(e -> e.getBedStatus() == 1).count());
        if (totalBeds != 0 && occupiedBeds != 0) {
            roomVo.setOccupancyRate(totalBeds * 1.0 / occupiedBeds);
        }else {
            roomVo.setOccupancyRate(0.0);
        }
        roomVo.setTotalBeds(totalBeds);
        roomVo.setOccupiedBeds(occupiedBeds);
        //4.床位集合
        List<BedVo>bedVoList=beds.stream().map(e->{
            //4.1创建bedvo
            BedVo bedVo=new BedVo();
            //4.2床位编号
            bedVo.setBedNumber(e.getBedNumber());
            //4.3服务等级  从入住配置表checkinconfig表中获取
            LambdaQueryWrapper<CheckInConfig> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(CheckInConfig::getBedNumber,e.getBedNumber());
            //获取服务等级
            CheckInConfig checkInConfig = checkInConfigMapper.selectOne(queryWrapper1);
            //查询服务等级名称 入住开始时间和结束时间
            if(checkInConfig!=null){
                bedVo.setLname(checkInConfig.getNursingLevelName());
                bedVo.getCheckInConfigVo().setCheckInStartTime(checkInConfig.getCheckInStartTime());
                bedVo.getCheckInConfigVo().setCheckInEndTime(checkInConfig.getCheckInEndTime());
            }
            //获取老人信息
            LambdaQueryWrapper<Elder> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(Elder::getBedId,e.getId());
            Elder elder=elderMapper.selectOne(queryWrapper2);

            if (elder!=null){
                bedVo.setName(elder.getName());
            }

            //返回vo
            return bedVo;
        }).collect(Collectors.toList());//收集成list
        roomVo.setBedVoList(bedVoList);
        return roomVo;
    }


}
